import { Component, OnInit } from '@angular/core';
import {Globals} from '../../../globals';
import {PageHelper} from '../../../utils/page-helper-model';
import {FormBuilder, FormGroup} from '@angular/forms';
import {RoleIdEnum} from '../../../enums/role-id.enum';
import {UserInfoModel} from '../../../models/user-info.model';
import {ApiUrl} from '../../../config/api-url';
import {Consts} from '../../../config/consts';
import {BaseService} from '../../../shared/service/base.service';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-device-annual-fee',
  templateUrl: './device-annual-fee.component.html',
  styleUrls: ['./device-annual-fee.component.less']
})
export class DeviceAnnualFeeComponent implements OnInit {
  isMobile = Globals.isMobile();
  listOfData: any;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  // 模态框表单
  formModel: FormGroup;
  roleIdEnum = {
    SUPER_ADMINISTRATOR: RoleIdEnum.SUPER_ADMINISTRATOR,
    AGENT: RoleIdEnum.AGENT,
    MERCHANT: RoleIdEnum.MERCHANT,
  };
  // 门店
  merchantStoreList: [];

  // 机构
  agencyList: [];
  // 运营商
  userList: [];
  // 用户信息
  userInfo: UserInfoModel;
  constructor(private baseService: BaseService, private activatedRoute: ActivatedRoute, private fb: FormBuilder , private router: Router) {
    this.buildForm();
  }
  ngOnInit(): void {
    this.userInfo = JSON.parse(sessionStorage.getItem(Consts.USER_INFO));
    this.getAllUserOfName();
    this.getAllAgencyOfName();
    this.findAllMerchantStoreByUserInfo();
    this.findDeviceAnnualFeeList();
  }
  buildForm() {
    this.formModel = this.fb.group({
      merchantStoreId: ['0', []],
      deviceNo: [null, []],
      createTime: [null, []],
      updateTime: [null, []],
      agencyId: ['', []],
      userId: ['', []],
    });
  }
  del(id){
    this.baseService.layer.modalDeleteConfirm('确定要删除这条网卡记录吗？', confirm => {
      if (confirm) {
        this.baseService.delete(ApiUrl.DeviceAnnualFeeUrl + '/delete', id).subscribe(
          data => {
            if (data.status == 0) {
              this.baseService.layer.messageSuccess("删除成功");
              this.findDeviceAnnualFeeList();
            } else {
              this.baseService.layer.messageError(data.msg);
            }
          }
        );
      }
    });
  }
  submitForm() {
    this.pageHelper.currentPage = 1;
    this.findDeviceAnnualFeeList();
  }
  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findDeviceAnnualFeeList();
  }
  onPageSizeChange(pageSize: any){
    this.pageHelper.pageSize = pageSize;
    this.pageHelper.currentPage = 1;
    this.findDeviceAnnualFeeList();
  }
  findDeviceAnnualFeeList(){
    this.baseService.findListByMuchCondition(ApiUrl.DeviceAnnualFeeUrl + '/findDeviceAnnualFeeList', this.formModel.value, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.listOfData = data.data.list;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }
  gainUserId($event: any) {
    this.findAllMerchantStoreByUserInfo();
  }
  getAllAgencyOfName() {
    this.baseService.get(ApiUrl.AgencyUrl + '/getAllAgencyOfName').subscribe(data => {
      if (data.status == 0) {
        this.agencyList = data.data;
      } else {
        this.baseService.layer.messageError(data.msg);
      }
    });
  }
  gainAgencyId($event: any) {
    this.getAllUserOfName();
  }
  getAllUserOfName() {
    let agencyId = this.formModel.get('agencyId').value;
    if (agencyId == null) {
      agencyId = '0';
    }
    this.baseService.get(ApiUrl.UserUrl + '/getAllUserOfName?agencyId=' + agencyId).subscribe(data => {
      if (data.status == 0) {
        this.userList = data.data;
      } else {
        this.baseService.layer.messageError(data.msg);
      }
    });
  }
  findAllMerchantStoreByUserInfo() {
    let userId = null;
    if (this.formModel.get('userId').value != null) {
      userId = this.formModel.get('userId').value;
    } else {
      userId = '';
    }
    this.baseService.get(ApiUrl.MerchantStoreUrl + '/findAllMerchantStoreByUserInfo?userId=' + userId).subscribe(
      data => {
        if (data.status == 0) {
          this.merchantStoreList = data.data;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

}
